package com.example.keshe0337.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.keshe0337.R;
import com.example.keshe0337.entity.ParkingSpace;

import java.util.List;

public class ParkingSpaceAdapter extends ArrayAdapter<ParkingSpace> {

    private final Context context;
    private final List<ParkingSpace> parkingSpaces;

    public ParkingSpaceAdapter(Context context, List<ParkingSpace> parkingSpaces) {
        super(context, 0, parkingSpaces);
        this.context = context;
        this.parkingSpaces = parkingSpaces;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_parking_item, parent, false);
        }

        ParkingSpace parkingSpace = parkingSpaces.get(position); //若当前停车位信息为空直接跳过封
        if (parkingSpace == null) {
            return convertView;
        }
        TextView parkingNumberText = convertView.findViewById(R.id.textParkingNumber);
        TextView statusText = convertView.findViewById(R.id.textStatus);
        TextView typeText = convertView.findViewById(R.id.textType);

        parkingNumberText.setText(parkingSpace.getSpaceNum());
        statusText.setText(parkingSpace.isStatus() ? "空闲" : "占用");
        typeText.setText(parkingSpace.getType());

        return convertView;
    }

    public void update(List<ParkingSpace> newData) {
        clear();           // 清空旧数据
        addAll(newData);   // 添加新数据
    }


}
